package com.kevin.j2eeToolBox.patterns.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ConcretePrototype implements Iprototype {

	/**
	 * 
	 */
	private String name;// 姓名
	private String sex;// 性别
	private String age;// 年龄
	private WorkExperience Work;// 工作经历

	/**
	 * 构造函数
	 * 
	 * @param name
	 */
	public ConcretePrototype(String name) {
		this.name = name;
		this.Work = new WorkExperience();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eight.prototypedeepcopy2.Prototype#setPersonInfo(java.lang.String,
	 * java.lang.String)
	 */
	public void setPersonInfo(String sex, String age) {
		this.sex = sex;
		this.age = age;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * eight.prototypedeepcopy2.Prototype#setWorkExperience(java.lang.String,
	 * java.lang.String)
	 */
	public void setWorkExperience(String workdate, String company) {
		this.Work.setWorkdate(workdate);
		this.Work.setWorkcompany(company);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eight.prototypedeepcopy2.Prototype#display()
	 */
	public void display() {
		System.out.println("名字：" + name);
		System.out.println("性别:" + sex);
		System.out.println("年龄:" + age);
		System.out
				.println("工作经历：" + Work.getWorkdate() + Work.getWorkcompany());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#clone()
	 */
	public Object clone() {
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bos);
			oos.writeObject(this);

			// 将流序列化
			ByteArrayInputStream bis = new ByteArrayInputStream(
					bos.toByteArray());
			ObjectInputStream ois = new ObjectInputStream(bis);
			return ois.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;

		// ConcretePrototype cprottotype = (ConcretePrototype) super.clone();
		// Work = (WorkExperience) cprottotype.getWork().clone();
		// return cprottotype;

	}
}
